package com.sdk.springbootadvancedsearch.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdk.springbootadvancedsearch.entity.SchemaSampleModelsEntity;
import com.sdk.springbootadvancedsearch.mapper.SchemaSampleModelsMapper;
import com.sdk.springbootadvancedsearch.domain.request.QueryRequest;
import com.sdk.springbootadvancedsearch.util.DynamicQueryBuilder;
import com.sdk.springbootadvancedsearch.util.SortBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class QueryService {
    private final DynamicQueryBuilder queryBuilder;
    private final SortBuilder sortBuilder;
    private final SchemaSampleModelsMapper projectMapper;

    public Page<SchemaSampleModelsEntity> executeQuery(QueryRequest request) {
        // 1. 构建查询条件
        QueryWrapper<SchemaSampleModelsEntity> wrapper = queryBuilder.buildFilters(request);

        // 2. 构建排序条件
        sortBuilder.buildSorts(wrapper, request.getSorts());

        // 3. 创建分页对象
        Page<SchemaSampleModelsEntity> page = new Page<>(request.getPage(), request.getPageSize());

        // 4. 执行查询
        return projectMapper.selectPage(page, wrapper);
    }

    // 简单查询（无分页）
    public List<SchemaSampleModelsEntity> executeQueryWithoutPage(QueryRequest request) {
        QueryWrapper<SchemaSampleModelsEntity> wrapper = queryBuilder.buildFilters(request);
        sortBuilder.buildSorts(wrapper, request.getSorts());
        return projectMapper.selectList(wrapper);
    }
}
